﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using Unis.Framework.ORM.Model;
using Unis.Framework.Core.Utility;

namespace Unis.Framework.ORM.Common
{
    [Serializable]
    [KnownType(typeof(BaseModel))]
    public abstract class BaseModel : INotifyPropertyChanged, ICloneable
    {
        // Fields
        private EntityState _entityState;
        private bool _selected;

        // Methods
        protected BaseModel()
        {
        }

        /// <summary>
        /// 克隆一个新的对象，内部的值与原来对象相同
        /// </summary>
        /// <returns>克隆好的对象</returns>
        public object Clone()
        {
            return this.Clone<object>();
        }

        // Properties

        public EntityState EntityState
        {
            get { return this._entityState; }
            set { this._entityState = value; }
        }


        public bool Selected
        {
            get
            {
                return this._selected;
            }
            set
            {
                if (object.Equals(this._selected, value) != true)
                {
                    this._selected = value;
                    this.RaisePropertyChanged("Selected");
                }
            }
        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
